﻿using System;

namespace Queue
{
    public class Queue
    {
        public QueueNode Head { get; private set; }
        public QueueNode Tail { get; private set; }
        public int Count { get; private set; }

        public Queue()
        {
            this.Head = null;
            this.Tail = null;
        }

        public void Enqueue(object item)
        {
            QueueNode newNode = new QueueNode(item);

            if (this.Head == null)
            {
                this.Head = newNode;
                this.Tail = newNode;
            }
            else
            {
                this.Tail.Next = newNode;
                this.Tail = this.Tail.Next;
            }

            this.Count++;
        }

        public object Dequeue()
        {
            object result = null;

            if (this.Head != null)
            {
                result = this.Head.Data;

                this.Head = this.Head.Next;                

                this.Count--;
            }

            return result;
        }

        public void Display()
        {
            QueueNode current = this.Head;

            while (current != null)
            {
                Console.Write(string.Format("| {0} |", current.Data));
                current = current.Next;
            }

            Console.ReadLine();
            Console.Clear();
        }

        public void Clear()
        {
            this.Head = null;
            this.Tail = null;
            this.Count = 0;
        }
    }

    public class QueueNode
    {
        public object Data { get; set; }
        public QueueNode Next { get; set; }

        public QueueNode()
        {
            this.Data = null;
            this.Next = null;
        }

        public QueueNode(object data)
            : this()
        {
            this.Data = data;
        }
    }
}